package com.winter.service;

import com.google.common.base.Strings;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.winter.mapper.FinContentLikeInfoMapper;
import com.winter.mapper.FinDepositReceiptMapper;
import com.winter.mapper.FinFavInfoMapper;
import com.winter.mapper.IcompanyCardMapper;
import com.winter.mapper.UserInfoMapper;
import com.winter.model.FinContentLikeInfo;
import com.winter.model.FinDepositReceipt;
import com.winter.model.IcompanyCard;
import com.winter.model.UserInfo;
import com.winter.util.Constants;
import com.winter.util.Result;
import com.winter.util.ResultCode;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class FinDepositReceiptService {

    @Autowired
    private FinDepositReceiptMapper finDepositReceiptMapper;


    @Autowired
    public FinFavInfoMapper finFavInfoMapper;


    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private IcompanyCardMapper icompanyCardMapper;

    /**
     * 点赞
     */
    @Autowired
    private FinContentLikeInfoMapper finContentLikeInfoMapper;


    public Result list(Integer pageSize, Integer pageNumber, String direct, String rating, String deadline, String rate, String userNo, String sortName, String sort, String key) {

        if (pageNumber == null) {
            pageNumber = Constants.PAGENUMBER;
        }
        if (pageSize == null) {
            pageSize = Constants.PAGESIZE;
        }
        PageHelper.startPage(pageNumber, pageSize, false);

        List<FinDepositReceipt> finDepositReceipts = finDepositReceiptMapper.selectList(direct, rating, deadline, rate, userNo, sortName, sort, key);
        finDepositReceipts.forEach(f -> {

            String photoPath = null;
            String photoType = f.getPhotoType();
            if (!Strings.isNullOrEmpty(photoType)) {
                if (photoType.equals("0")) {
                    // 获取创建人头像
                    String createUser = f.getCreateUser();
                    UserInfo userInfo = userInfoMapper.selectByUserNo(createUser);
                    if (userInfo != null) {
                        String custImgUrl = userInfo.getCustImgUrl();
                        if (!Strings.isNullOrEmpty(custImgUrl)) {
                            f.setAvatar(userInfo.getCustImgUrl());
                        }
                    } else {
                        //(数据类型)(最小值+Math.random()*(最大值-最小值+1))
                        //(int)(1+Math.random()*(10-1+1))
                        int verycode = (int) (1 + Math.random() * (70 - 1 + 1));
                        photoPath = "/p2p/images/imgthumbs/headimg_" + verycode + ".jpg";
                        f.setAvatar(photoPath);
                    }
                } else {
                    //(数据类型)(最小值+Math.random()*(最大值-最小值+1))
                    //(int)(1+Math.random()*(10-1+1))
                    int verycode = (int) (1 + Math.random() * (70 - 1 + 1));
                    photoPath = "/p2p/images/imgthumbs/headimg_" + verycode + ".jpg";
                    f.setAvatar(photoPath);
                }
            }
            if (!Strings.isNullOrEmpty(userNo)) {
                getisLikeOrCollect(userNo, f);
            }
        });

        PageInfo<FinDepositReceipt> pageInfo = new PageInfo<>(finDepositReceipts);
        return Result.success(pageInfo);
    }


    /**
     * 新增
     */
    public Result insert(FinDepositReceipt finDepositReceipt) {
        if (finDepositReceipt == null) {
            return Result.failure(ResultCode.PARAM_IS_BLANK, "参数列表为空");
        }
        if (Strings.isNullOrEmpty(finDepositReceipt.getCreateUser())) {
            return Result.failure(ResultCode.USER_NOT_LOGGED_IN);
        }
        if (Strings.isNullOrEmpty(finDepositReceipt.getDirect())) {
            return Result.failure(ResultCode.PARAM_IS_BLANK, "请选择方向");
        }
        if (Strings.isNullOrEmpty(finDepositReceipt.getRating())) {
            return Result.failure(ResultCode.PARAM_IS_BLANK, "请选择评级");
        }
        if (Strings.isNullOrEmpty(finDepositReceipt.getProvider())) {
            return Result.failure(ResultCode.PARAM_IS_BLANK, "请输入公司简称");
        }
        if (Strings.isNullOrEmpty(finDepositReceipt.getAmount())) {
            return Result.failure(ResultCode.PARAM_IS_BLANK, "请输入规模");
        }
        if (Strings.isNullOrEmpty(finDepositReceipt.getContact())) {
            return Result.failure(ResultCode.PARAM_IS_BLANK, "请输入联系方式");
        }

        UserInfo userInfo = userInfoMapper.selectByUserNo(finDepositReceipt.getCreateUser());
        String orgNo = userInfo.getOrgNo();
        IcompanyCard icompanyCard = icompanyCardMapper.selectByPrimaryKey(Long.valueOf(orgNo));
        // 公司信息
        finDepositReceipt.setUserName(userInfo.getUserName());
        finDepositReceipt.setUserCom(icompanyCard.getCompanyName());
        finDepositReceipt.setUserDepart(userInfo.getDepart());
        finDepositReceipt.setUserPost(userInfo.getPost());
        finDepositReceipt.setUserEmail(userInfo.getOrgEmail());
        finDepositReceipt.setUserQq(userInfo.getQq() + "");
        finDepositReceipt.setUserWx(userInfo.getWxNo());
        finDepositReceipt.setTopTime(new Date());
        finDepositReceipt.setInfoSrc("0");
        finDepositReceipt.setPhotoType("0");
        // 组装数据
        finDepositReceipt.setPhotoType("0");
        finDepositReceipt.setStas("3");
        finDepositReceipt.setCreateTime(new Date());
        finDepositReceipt.setUpdateTime(new Date());
        finDepositReceipt.setIsvalid("1");
        finDepositReceipt.setAuditing("0");
        finDepositReceipt.setCommentCount(0L);
        finDepositReceipt.setFavCount(0L);
        finDepositReceipt.setLikeCount(0L);
        finDepositReceipt.setCommentFlag("1");
        finDepositReceipt.setLikeFlag("1");
        finDepositReceipt.setReplayFlag("1");

        finDepositReceipt.setUpdateUser(finDepositReceipt.getCreateUser());
        finDepositReceiptMapper.insertSelective(finDepositReceipt);
        return Result.success();
    }

    public Result details(Long id, String userNo) {
        FinDepositReceipt f = finDepositReceiptMapper.selectByPrimaryKey(id);

        if (!Strings.isNullOrEmpty(userNo)) {
            getisLikeOrCollect(userNo, f);
        }
        return Result.success(f);
    }

    private void getisLikeOrCollect(String userNo, FinDepositReceipt f) {
        f.setIsCollect("0");
        f.setIsLike("0");
        // 点赞
        List<FinContentLikeInfo> isLike = finContentLikeInfoMapper.getIsLike(f.getId(), "1", "7", userNo);
        if (isLike.size() > 0) {
            f.setIsLike("1");
        }
        // 收藏
        Long aLong = finFavInfoMapper.get(f.getId(), "1", 7, userNo);
        if (aLong > 0) {
            f.setIsCollect("1");
        }
    }

    public Result update(FinDepositReceipt finDepositReceipt) {
        if (finDepositReceipt == null) {
            return Result.failure(ResultCode.PARAM_IS_BLANK, "参数列表为空");
        }
        if (Strings.isNullOrEmpty(finDepositReceipt.getCreateUser())) {
            return Result.failure(ResultCode.USER_NOT_LOGGED_IN);
        }
        if (finDepositReceipt.getId() == null) {
            return Result.failure(ResultCode.PARAM_IS_BLANK, "主键id不能为空");
        }
        if (Strings.isNullOrEmpty(finDepositReceipt.getDirect())) {
            return Result.failure(ResultCode.PARAM_IS_BLANK, "请选择方向");
        }
        if (Strings.isNullOrEmpty(finDepositReceipt.getRating())) {
            return Result.failure(ResultCode.PARAM_IS_BLANK, "请选择评级");
        }
        if (Strings.isNullOrEmpty(finDepositReceipt.getProvider())) {
            return Result.failure(ResultCode.PARAM_IS_BLANK, "请输入公司简称");
        }
        if (Strings.isNullOrEmpty(finDepositReceipt.getAmount())) {
            return Result.failure(ResultCode.PARAM_IS_BLANK, "请输入规模");
        }
        if (Strings.isNullOrEmpty(finDepositReceipt.getContact())) {
            return Result.failure(ResultCode.PARAM_IS_BLANK, "请输入联系方式");
        }
        finDepositReceipt.setUpdateTime(new Date());
        finDepositReceiptMapper.updateByPrimaryKeySelective(finDepositReceipt);
        return Result.success("修改成功");
    }
}
